#!/bin/bash

# Slackware build script for libfishsound

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20191213 bkw: New package.
# This only exists as a required dep for sonic-visualiser >= 4.0.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=libfishsound
VERSION=${VERSION:-1.0.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

sh autogen.sh

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --disable-static \
  --enable-shared \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG

# PV says we don't need these, and I agree
rm -f $PKG/usr/lib$LIBDIRSUFFIX/*.la

# --docdir accepted and ignored. also, don't install the latex docs,
# they're the source for the html docs. NEWS is 0 bytes, don't install.
DOCDIR=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $DOCDIR
mv $PKG/usr/share/doc/$PRGNAM/html $DOCDIR
rm -rf $PKG/usr/share/doc
cp -a AUTHORS COPYING ChangeLog README release_notes \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

if ldd $PKG/usr/lib$LIBDIRSUFFIX/$PRGNAM.so.*.*.* | grep -q speex; then
  WITHSPEEX=with
else
  WITHSPEEX=without
fi

# Install the examples, source only.
mkdir -p $DOCDIR/examples
cp -a src/examples/*.c $DOCDIR/examples

# Let the user know whether the optional speex dep is required. The other
# audio codec dependencies (libogg, flac, libvorbis) are part of a full
# Slackware install, we'll assume either they exist or the user knows
# what he's doing.
mkdir -p $PKG/install
sed "s,@WITHSPEEX@,$WITHSPEEX," $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
